// 生成uuid
export function guid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
}

// 防抖
export function debounce(callback) {
  let timer = null;
  return function(event, delay = 200) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      callback(event)
    }, delay)
  }
}

// 节流
export function throttle(callback) {
  let timer = null;
  return function(delay) {
    if (timer) return
    timer = setTimeout(() => {
      callback()
      timer = null;
    }, delay)
  }
}

// 根据标识生成对应的标签
export function transToTag(content, size = 24) {
  let reg = /\[([a-z]+)-([0-9]+)\]/g;
  return content.replace(reg, (res, $1, $2) => {
    switch ($1) {
      case 'emoji':
        return `<img src="" alt="[emoji-${$2}]" style="height: ${size-0.083*size}px;width: ${size}px;background: url(${require("@/assets/emoji/emoji.png")});background-repeat: no-repeat;background-size: ${size}px;margin:0 1px;background-position:0 ${-size * $2}px;vertical-align: text-bottom;" draggable="false">`
    }
  })
}